<?php


/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onAfterViewTicket', 'plgHelpDeskGoogleTranslate_onAfterViewTicket' );
$_MAMBOTS->registerFunction( 'onAfterEditTicket', 'plgHelpDeskGoogleTranslate_onAfterEditTicket' );

	/**
	 * Executes the plugin
	 * @return 
	 */
	function plgHelpDeskGoogleTranslate_onAfterViewTicket( $row, $user ) {
		$return = plgHelpDeskGoogleTranslate::onAfterViewTicket( $row, $user );
		return $return;
	}
	
	/**
	 * Executes the plugin
	 * @return 
	 */
	function plgHelpDeskGoogleTranslate_onAfterEditTicket( $row, $user ) {
		$return = plgHelpDeskGoogleTranslate::onAfterEditTicket( $row, $user );
		return $return;
	}
	
	class plgHelpDeskGoogleTranslate 
	{
		/**
		 * Gets a parameter value
		 *
		 * @access public
		 * @return mixed Parameter value
		 * @since 1.5
		 */
		function _getParameter( $name, $default=null ) 
		{
			static $params;
			
			if (!is_object($params))
			{
				$database = HelpDeskConfig::getDBO();
				$folder = strtolower( 'HelpDesk' );
				$element = strtolower( 'googletranslate' );
				
				$return = "";
		
				$query = "
					SELECT 
						`params`
					FROM 
						#__mambots
					WHERE 
						`element` = '{$element}'
					AND 
						`folder` = '{$folder}'
				";
				$database->setQuery( $query );
				$mambot = $database->loadObject();
				
				$params = new mosParameters( $mambot->params );			
			}
			
			$return = $params->get( $name, $default );
			return $return;
		}
		
		/**
		 * 
		 * Method is called after ticket is displayed on the front end
		 *
		 */
		function onAfterViewTicket() 
		{
			$mainframe = HelpDeskConfig::getApplication();
			$success = false;
			$html = '';
			
			$adminside = plgHelpDeskGoogleTranslate::_getParameter( 'adminside', '1');
			$frontend = plgHelpDeskGoogleTranslate::_getParameter( 'frontend', '1');
			
			if ($mainframe->isAdmin() && !$adminside)
			{
				return $success;
			}
	
			if ($mainframe->isSite() && !$frontend)
			{
				return $success;
			}
		
			// if we're viewing a ticket, set html
			$html = plgHelpDeskGoogleTranslate::renderHtml();
			
			echo $html;
			
			return $success;
		}
	
		/**
		 * 
		 * Method is called after ticket is displayed on the front end
		 *
		 */
		function onAfterEditTicket() 
		{
			$mainframe = HelpDeskConfig::getApplication();
			$success = false;
			$html = '';
			
			$adminside = plgHelpDeskGoogleTranslate::_getParameter( 'adminside', '1');
			$frontend = plgHelpDeskGoogleTranslate::_getParameter( 'frontend', '1');
			
			if ($mainframe->isAdmin() && !$adminside)
			{
				return $success;
			}
	
			if ($mainframe->isSite() && !$frontend)
			{
				return $success;
			}
			
			// if we're viewing a ticket, set html
			$html = plgHelpDeskGoogleTranslate::renderHtml();
			
			echo $html;
			
			return $success;
		}
			
		/**
		 * 
		 * @return HTML
		 */
		function renderHtml() 
		{
			$html = '';
			
			// display title
			$title = HelpDeskText::_( 'Google Translate' );
			
			$html .= '<p>
					<table class="userlist">
						<thead>
							<tr>
								<th class="title">'.
								$title
								.'</th>
							</tr>
						</thead>
					</table>
					</p>
					  ';
					  		
			//include the necessary javascript
			$html .= "<script language='javascript' type='text/javascript' src='http://www.google.com/jsapi?key=internal'></script>";
			$html .= "<script language='javascript' type='text/javascript' src='".HelpDeskUrl::root()."/mambots/helpdesk/googletranslate.js'></script>";
	  		
			//build the html form that references the google jsapi and the js
			$html .= '<form onsubmit="return submitChange();">';
	  		$html .= '<h4>'.HelpDeskText::_( 'Input' ).':</h4> ';
	  		$html .= '<textarea id="source" style="width:98%;" rows="5">'.HelpDeskText::_('Enter Text to Translate Here').'</textarea><br /><br />';
	  		$html .= HelpDeskText::_( 'From' ).': ';
	  		$html .= '<select name="src" id="src"></select> '; 
	  		$html .= HelpDeskText::_( 'To' ).': ';
	  		$html .= '<select name="dst" id="dst"></select> ';
	  		$html .= '<input class="button" type="submit" value="'.HelpDeskText::_('Translate').'"/></form><br />';
	  		$html .= '<h4>'.HelpDeskText::_( 'Translation' ).':</h4> ';  
	  		$html .= '<hr/><div id="results_body"></div>';
	  		$html .= '<div id="branding"></div>';
			
			return $html;
		}
	
	}